package com.zeng.jiudian.service.impl;

import com.zeng.jiudian.bean.NewModel;
import com.zeng.jiudian.dao.NewDao;
import com.zeng.jiudian.dao.impl.NewDaoImpl;
import com.zeng.jiudian.service.NewService;
import redis.clients.jedis.Jedis;

import java.util.List;

/**
 * @Author JJ_yo
 * @Date 2020/7/27 - 15:13
 */
public class NewServiceImpl implements NewService
{
    @Override
    public int add(NewModel newModel)
    {
        NewDao newDao = new NewDaoImpl();

        int count = newDao.add(newModel);
        if (count == 1)
        {
            Jedis jedis = new Jedis();
            jedis.select(0);
            jedis.flushDB();
        }

        return count;
    }

    @Override
    public List<NewModel> findAll(NewModel newModel)
    {
        NewDao newDao = new NewDaoImpl();

        return newDao.findAll(newModel);
    }

    @Override
    public int getCount(NewModel newModel)
    {
        NewDao newDao = new NewDaoImpl();

        return newDao.getCount(newModel);
    }

    @Override
    public int delete(int id)
    {
        NewDao newDao = new NewDaoImpl();

        int count = newDao.delete(id);
        if (count == 1)
        {
            Jedis jedis = new Jedis();
            jedis.select(0);
            jedis.flushDB();
        }

        return count;
    }

    @Override
    public NewModel findById(int id)
    {
        NewDao newDao = new NewDaoImpl();

        return newDao.findById(id);
    }

    @Override
    public int update(NewModel newModel)
    {
        NewDao newDao = new NewDaoImpl();

        if ("".equals(newModel.getTitle()))
        {
            newModel.setTitle(null);
        }
        if ("".equals(newModel.getIsFrom()))
        {
            newModel.setIsFrom(null);
        }
        if ("".equals(newModel.getAuthor()))
        {
            newModel.setAuthor(null);
        }
        if ("".equals(newModel.getType()))
        {
            newModel.setType(null);
        }
        if ("".equals(newModel.getContent()))
        {
            newModel.setContent(null);
        }
        if ("".equals(newModel.getIntroduction()))
        {
            newModel.setIntroduction(null);
        }

        int count = newDao.update(newModel);
        if (count == 1)
        {
            Jedis jedis = new Jedis();
            jedis.select(0);
            jedis.flushDB();
        }

        return count;
    }

    @Override
    public List<NewModel> findNewByRecommend()
    {
        NewDao newDao = new NewDaoImpl();

        return newDao.findNewByRecommend();
    }

    @Override
    public List<NewModel> findFacByRecommend()
    {
        NewDao newDao = new NewDaoImpl();

        return newDao.findFacByRecommend();
    }
}
